package cn.ffcs.uec.post.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.post.dao.UecPostDao;
import cn.ffcs.uec.post.model.UecPost;
import cn.ffcs.uec.post.vo.BasePostVo;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecPostDao")
@Transactional
public class UecPostDaoImpl extends BaseDaoImpl implements UecPostDao {

	@Override
	public PageInfo queryPageInfoByUecPost(UecPost uecPost, int current, int pageSize) {
		if (null != uecPost) {
			String orgName = PermissionUtil.getManagerOrg();
			StringBuilder sb = new StringBuilder(
					" SELECT T.* FROM UEC_POST T LEFT JOIN UEC_ORG_ORGS T1 ON(T.PK_ORG = T1.PK_ORG) "
					+ " WHERE T.STATUS_CD = ? AND T1.STATUS_CD = ? AND T.PK_POSTSTD IS NOT NUll ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if (null != uecPost) {
				if (!StrUtil.isEmpty(uecPost.getPostname())) {
					sb.append(" AND T.POSTNAME like ? ");
					params.add("%" + StringEscapeUtils.escapeSql(uecPost.getPostname()) + "%");
				}
				if (!StrUtil.isEmpty(uecPost.getPostcode())) {
					sb.append(" AND T.POSTCODE = ? ");
					params.add(uecPost.getPostcode());
				}
				if (uecPost.getPkOrg() != null) {
					sb.append(" AND T.PK_ORG = ? ");
					params.add(uecPost.getPkOrg());
				}
				if (uecPost.getPkDept() != null) {
					sb.append(" AND T.PK_DEPT = ? ");
					params.add(uecPost.getPkDept());
				}
				if (!"区本部".equals(orgName)) {
					sb.append(" AND T1.Name LIKE ? ");
					params.add("%" + StringEscapeUtils.escapeSql(orgName) + "%");
				}
				return this.jdbcFindPageInfo(sb.toString(), params, current, pageSize, UecPost.class);
			}
		}
		return null;
	}

	@Override
	public UecPost queryUecPost(UecPost uecPost) {
		if (null != uecPost) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_POST T WHERE T.STATUS_CD = ? AND T.PK_POSTSTD IS NOT NUll ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if (null != uecPost.getPkPost()) {
				sb.append(" AND T.PK_POST = ? ");
				params.add(uecPost.getPkPost());
			}
			List<UecPost> list = super.jdbcFindList(sb.toString(), params, UecPost.class);
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public UecPost queryBasePostByUecPost(UecPost uecPost) {
		if (null != uecPost) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_POST T WHERE T.STATUS_CD = ? AND T.PK_POSTSTD IS NUll ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if (null != uecPost.getPkPoststd()) {
				sb.append(" AND T.PK_POST = ? ");
				params.add(uecPost.getPkPoststd());
			}
			List<UecPost> list = super.jdbcFindList(sb.toString(), params, UecPost.class);
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public String getUecPostNameByPkPost(String pkPost) {
		if (!StrUtil.isEmpty(pkPost)) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_POST T WHERE T.STATUS_CD = ? AND T.PK_POSTSTD IS NOT NUll ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			sb.append(" AND T.PK_POST = ? ");
			params.add(pkPost);
			List<UecPost> list = super.jdbcFindList(sb.toString(), params, UecPost.class);
			if (list != null && list.size() > 0) {
				return list.get(0).getPostname();
			}
		}
		return "";
	}
	
	@Override
	public String getUecPostNameByPkBostPost(String pkBastPost) {
		if (!StrUtil.isEmpty(pkBastPost)) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_POST T WHERE T.STATUS_CD = ? AND T.PK_POSTSTD IS NUll ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			sb.append(" AND T.PK_POST = ? ");
			params.add(pkBastPost);
			List<UecPost> list = super.jdbcFindList(sb.toString(), params, UecPost.class);
			if (list != null && list.size() > 0) {
				return list.get(0).getPostname();
			}
		}
		return "";
	}

	@Override
	public BasePostVo getPreviousBasePostByCode(String code) {
		if (!StrUtil.isEmpty(code)) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM BASE_POST WHERE CODE = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(code);
			List<BasePostVo> list = super.jdbcFindList(sb.toString(), params, BasePostVo.class);
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}
	
	@Override
	public String getPreviousCodeByCode(String code) {
		if (!StrUtil.isEmpty(code)) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM BASE_POST WHERE CODE = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(code);
			List<BasePostVo> list = super.jdbcFindList(sb.toString(), params, BasePostVo.class);
			if (list != null && list.size() > 0) {
				return list.get(0).getPcode();
			}
		}
		return "";
	}

	@Override
	public UecPost getPkPostByCode(String code) {
		if (!StrUtil.isEmpty(code)) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_POST T WHERE T.STATUS_CD = ? AND T.PK_POSTSTD IS NUll ");
			List<Object> params = new ArrayList<Object>();
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			sb.append(" AND T.POSTCODE = ? ");
			params.add(code);
			List<UecPost> list = super.jdbcFindList(sb.toString(), params, UecPost.class);
			if (list != null && list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}

}
